package com.rbac.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbac.common.utils.R;
import com.rbac.modules.system.dto.UserDTO;
import com.rbac.modules.system.dto.UserQuery;
import com.rbac.modules.system.entity.SysPermission;
import com.rbac.modules.system.entity.SysUser;
import com.rbac.modules.system.service.SysPermissionService;
import com.rbac.modules.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "系统管理-用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/system/users")
public class SysUserController {

    @Resource
    private SysUserService userService;



    @Operation(summary = "分页查询用户列表", description = "根据条件分页查询用户列表")
    @GetMapping("/page")
    @SaCheckPermission(value = "system:user:list", orRole = "admin")
    public R<Page<SysUser>> page(
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "真实姓名") @RequestParam(required = false) String realName,
            @Parameter(description = "手机号") @RequestParam(required = false) String phone,
            @Parameter(description = "邮箱") @RequestParam(required = false) String email,
            @Parameter(description = "状态（0：禁用，1：启用）") @RequestParam(required = false) Integer status,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "排序字段") @RequestParam(required = false) String orderField,
            @Parameter(description = "排序方式（asc：升序，desc：降序）") @RequestParam(required = false) String orderType) {
        UserQuery query = new UserQuery();
        query.setUsername(username);
        query.setRealName(realName);
        query.setPhone(phone);
        query.setEmail(email);
        query.setStatus(status);
        query.setCurrent(current);
        query.setSize(size);
        query.setOrderField(orderField);
        query.setOrderType(orderType);
        return R.ok(userService.page(query));
    }

    @Operation(summary = "创建用户", description = "创建新的用户")
    @PostMapping
    @SaCheckPermission(value = "system:user:add", orRole = "admin")
    public R<Void> create(@Valid @RequestBody UserDTO userDTO) {
        userService.create(userDTO);
        return R.ok();
    }

    @Operation(summary = "更新用户", description = "根据ID更新用户信息")
    @PutMapping
    @SaCheckPermission(value = "system:user:edit", orRole = "admin")
    public R<Void> update(@Valid @RequestBody UserDTO userDTO) {
        userService.update(userDTO);
        return R.ok();
    }

    @Operation(summary = "删除用户", description = "根据ID删除用户")
    @DeleteMapping("/{ids}")
    @SaCheckPermission(value = "system:user:delete", orRole = "admin")
    public R<Void> delete(@Parameter(description = "用户ID数组") @PathVariable Long[] ids) {
        userService.delete(ids);
        return R.ok();
    }

    @Operation(summary = "重置密码", description = "重置指定用户的密码")
    @PutMapping("/password/reset/{ids}")
    @SaCheckPermission(value = "system:user:reset-password", orRole = "admin")
    public R<Void> resetPassword(@Parameter(description = "用户ID数组") @PathVariable Long[] ids) {
        userService.resetPassword(ids);
        return R.ok();
    }

}
